$(function(){
    $('#truename,#workname,#province,#company,#mobile,#address').bind({
        blur:function(){
            FormValidation(this);
        },
        focus:function(){
            var _tipsbox=$(this).nextAll('label.tips');
            _tipsbox.removeClass('moren');
            if(_tipsbox.hasClass('eer')){
                                    
            }else{
                _tipsbox.addClass('default');
            }
        }
    });
    $('button[id="reg_submit_btn"]').click(function() {
        var r = reg_validation();
        if(r == true){                                     
            $('form').submit();
        }
    });
});
					
/*确认方法*/
function reg_validation(){
	var gra_val = '';
    $.each($('input[id="grade_ids"]:checked'), function() {
     	gra_val = gra_val + ',' + $(this).attr('value');
    });
    if (gra_val == ''){
        $("#grade_tip").html('请选择您所教的年级');
    }
    
    var sub_val = '';
    $.each($('input[id="subject_ids"]:checked'), function() {
    	sub_val = sub_val + ',' + $(this).attr('value');
    });
    if (sub_val == ''){
        $("#subject_tip").html('请选择您所教的学科');
    }
        
    var _err=$('#userreg').find('label.tips');
    if(!_err.hasClass('eer')){
        FormValidation($('#province')[0]);
        FormValidation($('#truename')[0]);
        FormValidation($('#workname')[0]);
        FormValidation($('#mobile')[0]);
        FormValidation($('#company')[0]);
        FormValidation($('#address')[0]);		 
    }
    if(_err.hasClass('eer')){	
        return false;
    }else{
        return true;
    }
}
function setState (obj,cls,text){
    var _tips=$(obj).nextAll('label.tips');
    var _defalut_text=_tips.text();
    _tips.attr('class','tips '+cls);
    _tips.find('span').hide();
    _tips.find('label').remove();
    _tips.text('');
    if(cls=='eer'){
        _tips.addClass('err-back');
    }else{
        _tips.removeClass('err-back');
    }
    _tips.append(text); 
};
                    
function checkForm(obj,func){
    var rules={
        email 		: "^[A-Za-z0-9\d]+([-_\.\+]*[A-Za-z0-9\d])*@([a-z0-9\d][a-z0-9\d-]{0,61}[a-z0-9\d]\.)+[a-z\d]{2,6}$", 
        required	: "^.+$",
        password	: "^.+$",
        trueName	: '',
        province    : '',
        address     : '',
        company     : '',
        certificate : '',
        mobile		: "^1[3|5|8][0-9][0-9]{8}$",
        isCN		: '^[chr(0xa1)-chr(0xff)]+$',
        numeric		: "^[0-9]+$",
        loginname	: "^[a-z][a-z0-9-_.]+$",
        phone		: "^([0-9]{0}|[0-9]{3,4})[-]{0,1}[0-9]{7,8}$",
        idcard		: "^([0-9]{15}|[0-9]{18})$",
        zip			: "^[0-9]{6}$"
    };
    var arg=arguments;
    if(arg.length>0){
        var reg	= new RegExp(rules[obj.type] , "gi");
        var result	= reg.test(obj.value)?true:false;
        if(arg.length>1&&typeof(arg[1])=='function'){ 
            return func(result);
        }else{
            return result;
        }
    }else{
        alert('请设置您要检查的内容');
    }
};
function checkPassword(p1,p2){
    if(p1.value.length>5&&p1.value.length<17){ 
        if(p2.value.length>0){ 
            var _match=p1.value==p2.value?'':'请核对前后密码是否一致';
            if(p1.value==p2.value){
                setState(p1,'suc','');
            }else{
                setState(p1,'eer',_match);
            }
        }else{
            setState(p1,'suc','');
        }
    }else{
        setState(p1,'eer','6-16个字符，密码字母区分大小写');
    }
};

					
function checkDate(date)
{
    return true;
}
function FormValidation(n){
    var rules={
        truename:function(){
            if(n.value.length>1){ 
                checkForm({
                    'value':n.value,
                    'type':'isCN'
                },function(t){
                    if(t){
                        setState($('#truename')[0],'eer','请输入真实姓名');
                    }else{
                        setState($('#truename')[0],'suc','');
                    };
                });
            }else{
                setState($('#truename')[0],'eer','请输入真实姓名');
            }
        },
        workname:function(){
            var _address=$('#workname').val();
            if(_address!=''){ 
                setState($('#workname')[0],'suc','');
            }else{
                setState($('#workname')[0],'eer','请输入您的作品名');
            }
        },
        province:function(){
            var _province=$('#province').val();
            if(_province!=''){ 
                setState($('#province')[0],'suc','');
            }else{
                setState($('#province')[0],'eer','请输入联系地址');
            }
        },
        address:function(){
            var _address=$('#address').val();
            if(_address!=''){ 
                setState($('#address')[0],'suc','');
            }else{
                setState($('#address')[0],'eer','请输入联系地址');
            }
        },
        company:function(){
            if(n.value.length>1&&n.value.length<51){ 
                checkForm({
                    'value':n.value,
                    'type':'isCN'
                },function(t){
                    if(t){
                        setState($('#company')[0],'eer','请输入您所在的学校名 ');
                    }else{
                        setState($('#company')[0],'suc','');
                    };
                });
            }else{
                setState($('#company')[0],'eer','请输入真实工作单位 ');
            }
        },
        mobile:function(){
            checkForm({
                'value':n.value,
                'type':'mobile'
            },function(t){
                var _box=$('#mobile')[0];
                if(t){
                    setState($('#mobile')[0],'suc','');
                }else{
                    setState(_box,'eer','填写的手机号码错误');
                }
            });
        }
    };
    rules[n.id]();
};

